﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace zevenseas.Community.Utilities
{
    class ThemeFeatureBuilder
    {
        static void Main(string[] args)
        {       
            try
            {
                if (args.Length != 3)
                {
                    Usage();
                    return;
                }

                string ThemeName = args[0];
                string FeatureFolder = args[1];
                string ThemeFiles = args[2];
                string FeatureThemeFolder = FeatureFolder + @"\\Themes\\" + ThemeName + @"\\";

                CreateFeature(FeatureFolder, ThemeName);

                FileInfo f = new FileInfo(FeatureFolder + @"\\ThemeFiles.xml");
                StreamWriter w;
                w = f.CreateText();
                w.WriteLine(@"<Elements xmlns=""http://schemas.microsoft.com/sharepoint/"">");

                CreateFileModule(w, ThemeName, ThemeName, ThemeName, FeatureThemeFolder, ThemeFiles, ThemeName);
                CreateFileModule(w, ThemeName + @"\images", ThemeName + "/images", ThemeName + "Images", FeatureThemeFolder + @"\\images\\", ThemeFiles + @"\\images", ThemeName);

                w.WriteLine(@"</Elements>");
                w.Close();
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.ToString());
                Usage();
            }
        }

        private static void CreateFileModule(StreamWriter w, string Path, string Url, string ModuleName, string TargetFolder, string sSourceFolder, string ThemeName)
        {
            DirectoryInfo SourceFolder = new DirectoryInfo(sSourceFolder);
            FileInfo[] files = SourceFolder.GetFiles();

            w.WriteLine(@"  <Module Name=""" + ModuleName + @""" Url=""Themes/" + Url + @""" Path=""Themes\" + Path + @""" RootWebOnly=""FALSE"">");

            foreach (FileInfo tf in files)
            {
                if (!tf.Name.ToLower().Contains(".master"))
                {
                    w.WriteLine(@"      <File Url=""" + tf.Name + @""" Name=""" + tf.Name + @""" />");
                }
                tf.CopyTo(TargetFolder + tf.Name);
            }
            w.WriteLine(@"  </Module>");
        }       

        private static void CreateFeature(string FeatureFolder, string ThemeName)
        {
            Directory.CreateDirectory(FeatureFolder);
            Directory.CreateDirectory(FeatureFolder + @"\\Themes");
            Directory.CreateDirectory(FeatureFolder + @"\\Themes\\" + ThemeName);
            Directory.CreateDirectory(FeatureFolder + @"\\Themes\\" + ThemeName + @"\\images");

            FileInfo f = new FileInfo(FeatureFolder + @"\\Feature.xml");
            StreamWriter w = f.CreateText();

            w.WriteLine(@"<?xml version=""1.0"" encoding=""utf-8"" ?>");
            w.WriteLine(@"<Feature Id=""" + Guid.NewGuid() + @"""");
            w.WriteLine(@"  Title=""CKS:EBE Theme: " + ThemeName + @"""");
            w.WriteLine(@"  Description=""A Theme for the Commnity Kit for SharePoint: Enhanced Blog Edition called: " + ThemeName + @"""");
            w.WriteLine(@"  Version=""1.0.0.0""");
            w.WriteLine(@"  Scope=""Web""");
            w.WriteLine(@"  Hidden=""FALSE""");
            w.WriteLine(@"  ReceiverAssembly=""CKS.EBE, Version=0.1.0.0, Culture=neutral, PublicKeyToken=3e8b700c069fb747""");
            w.WriteLine(@"  ReceiverClass=""CKS.EBE.ThemeFeatureReceiver""");
            w.WriteLine(@"  xmlns=""http://schemas.microsoft.com/sharepoint/"">");
            w.WriteLine(@"  <ElementManifests>");
            w.WriteLine(@"      <ElementManifest Location=""ThemeFiles.xml""/>");
            w.WriteLine(@"  </ElementManifests>");
            w.WriteLine(@"</Feature>");
            w.Close();
        }

        static void Usage()
        {
            Console.WriteLine("");
            Console.WriteLine("CKS:EBE Theme Feature Generator");
            Console.WriteLine("-------------------------------");
            Console.WriteLine("Usage: zsThemeFeatureGenerator <ThemeName> <NewFeatureFolder> <CurrentThemeFolder>");
            Console.WriteLine("");
            Console.WriteLine("<ThemeName>          Name you would like to give the new Theme");
            Console.WriteLine("<NewFeatureFolder>   New folder where you would like to create the feature.");
            Console.WriteLine("<CurrentThemeFolder> Name of the folder where the current SPD created theme is located.");
            Console.WriteLine("");
            Console.WriteLine("The existing SharePoint Designer theme should have the following folder structure:");
            Console.WriteLine(@"\Theme");
            Console.WriteLine(@"\Theme\Images");
            Console.WriteLine("");
            Console.WriteLine("");
        }
    }
}
